package com.laowang.convert;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laowang.dataoject.OrderDetail;
import com.laowang.dto.OrderDTO;
import com.laowang.enums.ResultEnum;
import com.laowang.exception.SellException;
import com.laowang.form.OrderForm;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.description.method.MethodDescription;
import org.hibernate.cache.spi.SecondLevelCacheLogger;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/15.
 */
@Slf4j
public class OrderForm2OrderDTO {
    public static OrderDTO convert(OrderForm orderForm){
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenId());
        List<OrderDetail> orderDetailList = new ArrayList<>();
        //json转list
        try {
            orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>() {
            }.getType());

        }catch (Exception e){
            log.error("[对象转换]错误，string={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }


        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }


    }
